/**
 * ==================================================================================
 * Copyright (c) 2012 All Rights Reserved.
 * 
 * Author: 	Jye Cusch
 * Owner:	Team LAAC
 * Date:	04/11/2012
 * 
 * Summary:	Class for handling the management of book, including reading them from
 * 			a database and performing search functions.
 * 
 * ==================================================================================
 */

package au.com.somethingwritten.model;

import java.util.ArrayList;

public class BookManager {
	
	public BookManager(){
		//null
	}
	
	public ArrayList<Book> getAllBooks(){
		try{
			return BookDAO.getAllBooks();
		}catch (Exception e){
			//throw e;
		}
		return null;
	}
	
	public ArrayList<Book> findBooks(String input){
		ArrayList<Book> matchingBooks = new ArrayList<Book>();
		//if no input provided, return no results.
		if(input == "")
			return matchingBooks;
		
		//get all books
		ArrayList<Book> allBooks = getAllBooks();
		//find the list of matching books
		for(int i = 0; i < allBooks.size(); i++){
			if(allBooks.get(i).toString().toLowerCase().contains(input.toLowerCase())){
				matchingBooks.add(allBooks.get(i));
			}
		}
		//return matching books
		return matchingBooks;
	}
	
	public Book getBook(int id){
		//get all books
		ArrayList<Book> allBooks = getAllBooks();
		for(int i = 0; i < allBooks.size();i++){
			if(allBooks.get(i).getId() == id)
				//found
				return allBooks.get(i);
		}
		//not found.
		return null;
	}
	
	public ArrayList<Book> findBooksByISBN(String input){
		ArrayList<Book> matchingBooks = new ArrayList<Book>();
		//if no input provided, return no results.
		if(input == "")
			return matchingBooks;
		
		//get all books
		ArrayList<Book> allBooks = getAllBooks();
		//find the list of matching books
		for(int i = 0; i < allBooks.size(); i++){
			if(allBooks.get(i).getISBN().toLowerCase().contains(input.toLowerCase())){
				matchingBooks.add(allBooks.get(i));
			}
		}
		//return matching books
		return matchingBooks;
	}
	
	public ArrayList<Book> findBooksByTitle(String input){
		ArrayList<Book> matchingBooks = new ArrayList<Book>();
		//if no input provided, return no results.
		if(input == "")
			return matchingBooks;
		
		//get all books
		ArrayList<Book> allBooks = getAllBooks();
		//find the list of matching books
		for(int i = 0; i < allBooks.size(); i++){
			if(allBooks.get(i).getTitle().toLowerCase().contains(input.toLowerCase())){
				matchingBooks.add(allBooks.get(i));
			}
		}
		//return matching books
		return matchingBooks;
	}
	
	public ArrayList<Book> findBooksByAuthor(String input){
		ArrayList<Book> matchingBooks = new ArrayList<Book>();
		//if no input provided, return no results.
		if(input == "")
			return matchingBooks;
		
		//get all books
		ArrayList<Book> allBooks = getAllBooks();
		//find the list of matching books
		for(int i = 0; i < allBooks.size(); i++){
			if(allBooks.get(i).getAuthor().toLowerCase().contains(input.toLowerCase())){
				matchingBooks.add(allBooks.get(i));
			}
		}
		//return matching books
		return matchingBooks;
	}
	
	public ArrayList<Book> findBooksByCategory(String input){
		ArrayList<Book> matchingBooks = new ArrayList<Book>();
		//if no input provided, return no results.
		if(input == "")
			return matchingBooks;
		
		//get all books
		ArrayList<Book> allBooks = getAllBooks();
		//find the list of matching books
		for(int i = 0; i < allBooks.size(); i++){
			if(allBooks.get(i).getCategory().toLowerCase().contains(input.toLowerCase())){
				matchingBooks.add(allBooks.get(i));
			}
		}
		//return matching books
		return matchingBooks;
	}

}
